package cn.xiaohuodui.controller;

import cn.xiaohuodui.exceptions.BussException;
import cn.xiaohuodui.form.back.brand.BackBrandCreateForm;
import cn.xiaohuodui.form.back.brand.BackBrandDo;
import cn.xiaohuodui.form.back.brand.BackBrandQueryForm;
import cn.xiaohuodui.form.back.category.BackCategoryDo;
import cn.xiaohuodui.form.back.good.BackGoodDo;
import cn.xiaohuodui.form.back.good.BackGoodQueryForm;
import cn.xiaohuodui.form.back.sku.BackSkuAttributeCreateForm;
import cn.xiaohuodui.form.back.sku.BackSkuCreateForm;
import cn.xiaohuodui.form.back.sku.BackSkuDo;
import cn.xiaohuodui.form.back.sku.BackSkuQueryForm;
import cn.xiaohuodui.res.JsonRes;
import cn.xiaohuodui.service.BrandService;
import cn.xiaohuodui.service.CategoryService;
import cn.xiaohuodui.service.GoodService;
import cn.xiaohuodui.service.SkuService;
import cn.xiaohuodui.util.PageUtil;
import cn.xiaohuodui.utils.json.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import cn.xiaohuodui.form.back.good.BackGoodCreateForm;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商品控制器
 *
 * @author keepcleargas
 * @create 2017-05-04 上午9:33
 */
@Controller
@RequestMapping("/goods")
public class GoodController {
    @Autowired
    GoodService goodService;
    @Autowired
    SkuService skuService;
//    @Autowired
//    CategoryService categoryService;
    @Autowired
    BrandService brandService;



    @GetMapping
    public String index(Model model) {
        return "good/good-list";
    }
    @GetMapping("/add")
    public String add(Model model) {
        return "good/good-add";
    }

    /**
     * 新增商品
     *
     * @return
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public JsonRes createGood(
            @ModelAttribute BackGoodCreateForm form) throws BussException {
        goodService.createGood(form);
        return JsonRes.success();
    }

    /**
     * 商品列表
     *
     * @return
     */
    @GetMapping(value = "/page")
    @ResponseBody
    public String getPage(HttpServletRequest request) throws ParseException {
        List<Integer> list = PageUtil.pagePrepare(request);
        int limit = list.get(0).intValue();
        int offset = list.get(1).intValue();
        int draw = list.get(2).intValue();

        String name = request.getParameter("name");
        String num = request.getParameter("num");
        String brandIdStr = request.getParameter("brandId");
        String isOnSaleStr = request.getParameter("isOnSale");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String isNewStr = request.getParameter("isNew");
        String isHotStr = request.getParameter("isHot");

        int brandId = (brandIdStr == null || brandIdStr.equals("")) ? -1 : Integer.parseInt(brandIdStr);
        int isNew = (isNewStr == null || isNewStr.equals("")) ? -1 : Integer.parseInt(isNewStr);
        int isHot = (isHotStr == null || isHotStr.equals("")) ? -1 : Integer.parseInt(isHotStr);
        int isOnSale = (isOnSaleStr == null || isOnSaleStr.equals("")) ? -1 : Integer.parseInt(isOnSaleStr);

        BackGoodQueryForm form = new BackGoodQueryForm();
        form.setLimit(limit);
        form.setOffset(offset);

        form.setBrandId(brandId);
        if (endTime != null && !"".equals(endTime)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = formatter.parse(endTime);
            form.setEndTime(date.getTime());
        }
        if (startTime != null && !"".equals(startTime)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = formatter.parse(startTime);
            form.setStartTime(date.getTime());
        }
        form.setIsHot(isHot);
        form.setIsNew(isNew);
        form.setIsOnSale(isOnSale);
        form.setName(name);
        form.setNum(num);

        int count = goodService.goodBackCount(form);
        List<BackGoodDo> datas = goodService.goodBackQuery(form);

        return JsonUtil.seriazileAsStringWithNull(PageUtil.pageResult(draw, datas, count));
    }





    @GetMapping("/category")
    public String category(Model model) {
        return "good/good-category";
    }



    @GetMapping("/sku")
    public String sku(Model model) {
        return "good/good-sku";
    }




    /**
     * 新增SKU
     *
     * @return
     */
    @PostMapping(value = "/sku/add")
    @ResponseBody
    public JsonRes createSku(
            @ModelAttribute BackSkuCreateForm form1, @ModelAttribute BackSkuAttributeCreateForm form2) throws BussException {
        skuService.createSku(form1,form2);
        return JsonRes.success();
    }


    /**
     * sku列表
     *
     * @return
     */
    @GetMapping(value = "/sku/page")
    @ResponseBody
    public String getSkuPage(HttpServletRequest request) throws ParseException {
        List<Integer> list = PageUtil.pagePrepare(request);
        int limit = list.get(0).intValue();
        int offset = list.get(1).intValue();
        int draw = list.get(2).intValue();

        String name = request.getParameter("name");

        BackSkuQueryForm form = new BackSkuQueryForm();
        form.setLimit(limit);
        form.setOffset(offset);
        form.setName(name);

        int count = skuService.skuBackCount(form);
        List<BackSkuDo> datas = skuService.skuBackQuery(form);

        return JsonUtil.seriazileAsStringWithNull(PageUtil.pageResult(draw, datas, count));
    }


    /**
     * 新增brand
     *
     * @return
     */
    @PostMapping(value = "/brand/add")
    @ResponseBody
    public JsonRes createBrand(
            @ModelAttribute BackBrandCreateForm form) throws BussException {
        brandService.createBrand(form);
        return JsonRes.success();
    }




    @GetMapping("/brand")
    public String brand(Model model) {
        return "good/good-brand";
    }

    /**
     * 品牌列表
     *
     * @return
     */
    @GetMapping(value = "/brand/page")
    @ResponseBody
    public String getBrandPage(HttpServletRequest request) throws ParseException {
        List<Integer> list = PageUtil.pagePrepare(request);
        int limit = list.get(0).intValue();
        int offset = list.get(1).intValue();
        int draw = list.get(2).intValue();

        String name = request.getParameter("name");
        String enName = request.getParameter("enName");

        BackBrandQueryForm form = new BackBrandQueryForm();
        form.setLimit(limit);
        form.setOffset(offset);
        form.setName(name);
        form.setEnName(enName);


        int count = brandService.brandBackCount(form);
        List<BackBrandDo> datas = brandService.brandBackQuery(form);

        return JsonUtil.seriazileAsStringWithNull(PageUtil.pageResult(draw, datas, count));
    }








}
